package core

import (
	"fmt"
	"math/rand"
	"time"
)

type GuessNum struct {
	maxNumber    int
	minNumber    int
	targetNumber int
	guessNumber  int
	index        int
	guessCount	 int
}

func (gn *GuessNum) InitCount() {
	gn.index = 0
}

func (gn *GuessNum) SetMinNumber(num int) {
	gn.minNumber = num
}

func (gn *GuessNum) SetMaxNumber(num int) {
	gn.maxNumber = num
}

func (gn *GuessNum) GenerateTargetNumber() {
	rand.Seed(time.Now().Unix())
	if gn.maxNumber - gn.minNumber < 1 {
		fmt.Printf("范围错误：最小值：%d, 最大值：%d\n", gn.minNumber, gn.maxNumber)
	} else {
		gn.targetNumber = rand.Intn(gn.maxNumber-gn.minNumber) + gn.minNumber + 1
	}
}

//
func (gn *GuessNum) GetNumber() int {
	return gn.targetNumber
}

func (gn *GuessNum) GetGuess() int {
	return gn.guessNumber
}

func (gn *GuessNum) SetGuess(guess int) {
	gn.guessNumber = guess
}

func (gn *GuessNum) GetSmallest() int {
	return gn.minNumber
}

func (gn *GuessNum) GetBiggest() int {
	return gn.maxNumber
}

func (gn *GuessNum) GetCurrentGuessTime() int {
	return gn.index
}

func (gn *GuessNum) Reset() {

}

func (gn *GuessNum) Check() {

}

func (gn *GuessNum) IsValidNumberRange() bool {
	if gn.guessNumber >= gn.minNumber && gn.guessNumber <= gn.maxNumber {
		return true
	} else {
		return false
	}
}

func (gn *GuessNum) IsGameWon() bool { // 游戏是否成功
	if gn.index <= gn.guessCount {
		if gn.guessNumber == gn.targetNumber {
			return true
		} else {
			return false
		}
	} else {
		return false
	}
}

func (gn *GuessNum) IsGameLost() bool { // 游戏是否失败
	if gn.index >= gn.guessCount {
		return true
	} else {
		return false
	}
}

func (gn *GuessNum) CountInit() {
	gn.index = 0
}

func (gn *GuessNum) CountIncrease() {
	gn.index++
}


func NewNumberGenerator(minNumber, maxNumber int) NumberGenerator  {
	if minNumber < maxNumber {
		return &DefaultNumberGenerator{minNumber:minNumber, maxNumber:maxNumber}
	} else {
		return &DefaultNumberGenerator{minNumber:maxNumber, maxNumber:minNumber}
	}
}

func NewGame(count int, numGenerator NumberGenerator) GuessNum {
	return GuessNum{
		maxNumber:    numGenerator.GetMaxNumber(),
		minNumber:    numGenerator.GetMinNumber(),
		targetNumber: numGenerator.Next(),
		guessCount:   count,
	}

}